<!-- eslint-disable vue/valid-v-for -->
<template>
  <!-- 表单 -->
  <el-form
    ref="addForm"
    :model="formData"
    :rules="rules"
    label-width="120px"
  >
    <el-form-item
      label="姓名"
      prop="username"
    >
      <el-input
        v-model="formData.username"
        style="width:50%"
        placeholder="请输入姓名"
      />
    </el-form-item>
    <el-form-item
      label="手机"
      prop="mobile"
    >
      <el-input
        v-model="formData.mobile"
        style="width:50%"
        placeholder="请输入手机号"
      />
    </el-form-item>
    <el-form-item
      label="入职时间"
      prop="timeOfEntry"
    >
      <el-date-picker
        v-model="formData.timeOfEntry"
        style="width:50%"
        placeholder="请选择入职时间"
      />
    </el-form-item>
    <el-form-item
      label="聘用形式"
      prop="formOfEmployment"
    >
      <el-select
        v-model="formData.formOfEmployment"
        style="width:50%"
        placeholder="请选择"
      >
        <el-option
          v-for="(item,index) in hireType"
          :key="index"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item
      label="工号"
      prop="workNumber"
    >
      <el-input
        v-model="formData.workNumber"
        style="width:50%"
        placeholder="请输入工号"
      />
    </el-form-item>
    <el-form-item
      label="部门"
      prop="departmentName"
    >
      <el-input
        v-model="formData.departmentName"
        style="width:50%"
        placeholder="请选择部门"
        readonly
        @focus="departmentNameFoucs"
      />

      <div class="tree-box">
        <el-tree
          v-show="showTree"
          :data="treeData"
          default-expand-all
          :props="{ label: 'name' }"
          @node-click="treeClick"
        />
      </div>
    </el-form-item>
    <el-form-item
      label="转正时间"
      prop="correctionTime"
    >
      <el-date-picker
        v-model="formData.correctionTime"
        style="width:50%"
        placeholder="请选择转正时间"
      />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelFn">取消</el-button>
      <el-button
        type="primary"
        @click="confirmFn"
      >确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import Employees from '@/api/constant'
export default {
  name: 'EmployeeForm',
  props: {
    // 部门列表树形结构
    treeData: {
      type: Array,
      default: () => [] // 返回默认数组
    }
  },
  data() {
    return {
      // 添加表单字段
      formData: {
        username: '', // 用户名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      // 添加表单的校验字段
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '请选择入职时间', trigger: 'blur' }]
      },
      hireType: Employees.hireType, // 聘用形式枚举数组+对象
      showTree: false // 部门树形空间显示/隐藏
      // clickDepartName: '' // 点击的部门名字(选择)
    }
  },
  // watch: {
  //   检测用户在页面修改后赋予的新值newVal
  //   检测置空表单是也触发
  //   'formData.departmentName'(newVal) {
  //   if(newVal.length===0){  证明表单要置空
  //    return 组织代码往下执行
  // }
  //   if (newVal !== this.clickDepartName) {
  //  this.formData.departmentName = this.clickDepartName // 证明用户在输入框修改，立刻把用户改的覆盖掉
  //     }
  //   }
  // },
  methods: {
    // 点击取消事件
    cancelFn() {
      this.$emit('update:show', false)
    },
    // 点击确定事件
    confirmFn() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          // console.log(this.formData)能拿到表单提交的数据，所以在这里发请求，但是我们要在主页面发请求，所以把这个表单数据传给父组件
          // 清空表单时，要给axiox异步请求拷贝一份表单，他可能在置空后在执行
          this.$emit('addEmployeeEV', { ...this.formData })
          this.$emit('update:show', false)
        }
      })
    },
    // 部门输入框->聚焦事件
    departmentNameFoucs() {
      this.showTree = true
    },
    // 部门的树形控件行的点击事件
    // data：点击那一行对应的数据对象
    treeClick(data) {
      if (data && data.children) {
        return // 你点击在了折叠的行上(回去吧)
      }
      this.formData.departmentName = data.name
      // this.clickDepartName = data.name // 把点击的名字另存一份
      this.showTree = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
